package com.sds.thesis.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class JsonDataUtil {

    public void parserJson(String filePath) throws Exception{
        JSONObject jsonObject = readJsonFromFile(filePath);
        System.out.println("jsonObject=" + jsonObject);
    }

    /**
     * 读取json文件
     * @param filePath
     * @return
     */
    public static JSONObject readJsonFromFile(String filePath) {
        try (FileReader fileReader = new FileReader(filePath)) {
            StringBuilder stringBuilder = new StringBuilder();
            int character;
            while ((character = fileReader.read()) != -1) {
                stringBuilder.append((char) character);
            }
            return JSON.parseObject(stringBuilder.toString());
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("readJsonFromFile fail：" + e);
        }
    }
}
